package com.htandroid.htandroid.excel;

import android.content.Context;

import com.htandroid.htandroid.entity.MrqgdDjList;
import com.htandroid.htandroid.global.MyApplication;
import com.htandroid.htandroid.sys.Basenvo;

import java.io.File;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelPurchase_cfqgqd extends BaseExcel {
    private static WritableWorkbook wwb;
    private static String excelPath;
    private static String listtype;
    private static File excelFile;
    private static List<MrqgdDjList> list;

    public ExcelPurchase_cfqgqd(Context context, String excelPath, List<MrqgdDjList> list, String listtype) {
        this.excelPath = excelPath;
        this.list = list;
        this.listtype = listtype;
        excelFile = new File(excelPath);
        myApplication = (MyApplication) context.getApplicationContext();
        createSmtrautExcel(myApplication, excelFile, list, listtype);
    }

    public void createSmtrautExcel(MyApplication myApplication, File file, List<MrqgdDjList> list, String listtype) {
        format();
        WritableSheet ws = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            wwb = Workbook.createWorkbook(file);
            ws = wwb.createSheet("0".equals(listtype) ? "待叫货" : "未定价", 0);

            Label label = new Label(0, 0, myApplication.getGs_ztname(), arial14format);
            ws.addCell(label);

            String[] topic = {"部门", "存货编码", "存货名称", "规格", "单位", "要求到货日期", "请购数量",
                    "单价", "金额", "备注"};
            for (int i = 0; i < topic.length; i++) {
                Label labelC = new Label(i, 1, topic[i], arial12format);
                try {
                    ws.addCell(labelC);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Label labelcCdepname = new Label(0, i + 2, list.get(i).getCdepname());
                Label labelInvcode = new Label(1, i + 2, list.get(i).getcInvcode());
                Label labelcInvname = new Label(2, i + 2, list.get(i).getcInvname());//存货名称
                Label labelcInvstd = new Label(3, i + 2, list.get(i).getcInvstd());//规格
                Label labelcInvm_unit = new Label(4, i + 2, list.get(i).getcInvm_unit());//(主计量)单位
                Label labelcdDate = new Label(5, i + 2, list.get(i).getdDate());
                Label labeliQuantity = new Label(6, i + 2, Basenvo.space_add(Basenvo.customFormat("#####0.00##", list.get(i).getiQuantity()), 8, 0));
                Label labelIprice = new Label(7, i + 2, Basenvo.space_add(Basenvo.customFormat("#####0.00##", list.get(i).getIprice()), 12, 1));
                Label labelIamount = new Label(8, i + 2, Basenvo.space_add(Basenvo.customFormat("#####0.00##", list.get(i).getIamount()), 12, 0));
                Label labelCmemo = new Label(9, i + 2, list.get(i).getCmemo());
                try {
                    ws.addCell(labelcCdepname);
                    ws.addCell(labelInvcode);
                    ws.addCell(labelcInvname);
                    ws.addCell(labelcInvstd);
                    ws.addCell(labelcInvm_unit);
                    ws.addCell(labelcdDate);
                    ws.addCell(labeliQuantity);
                    ws.addCell(labelIprice);
                    ws.addCell(labelIamount);
                    ws.addCell(labelCmemo);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            ws.mergeCells(0, 0, topic.length - 1, 0);
            ws.setColumnView(0, 15);
            ws.setColumnView(1, 15);
            ws.setColumnView(2, 15);
            ws.setColumnView(5, 15);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wwb != null) {
                try {
                    wwb.write();
                    wwb.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
